package com.neusoft.elmboot.controller;


import com.github.pagehelper.PageInfo;
import com.neusoft.elmboot.common.Result;
import com.neusoft.elmboot.dto.BusinessReqDto;
import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商家管理模块-控制器
 */
@RestController
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 商家列表查询接口
     * @return
     */
    @GetMapping("/list")
    public Result list(BusinessReqDto params) {
        // 调用业务层（service层）来处理业务逻辑！
//        List<Business> businesssList = businessService.getBusinessList(params);
//        Long total = businessService.getBusinessCount(params);

        Map<String,Object> retMap = new HashMap<>();
//        retMap.put("data",businesssList);
//        retMap.put("total", total);

        PageInfo<Business> page = businessService.getBusinessPage(params);
        retMap.put("data",page.getList());
        retMap.put("total", page.getTotal());



        return Result.success(retMap,"商家列表查询成功！");
    }


    /**
     * 新增商家接口
     * @return
     */
    @PostMapping("/create")
    public Result add(@RequestBody BusinessReqDto params) {
        System.out.println("-----------business/create...");
        // 调用业务层（service层）来处理业务逻辑！
        Integer x = businessService.addBusiness(params);

        return Result.success(x,"新增商家成功！");
    }


    @GetMapping("/detail")
    public Result detail(Integer id) {
        Business business = businessService.getBusinessByBusinessId(id);
        return Result.success(business,"查看商家详情成功");
    }


    /**
     * 更新商家接口
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody BusinessReqDto params) {
        System.out.println("-----------business/update...");
        // 校验参数合法性  (也可以使用validator验证框架)
        if (null == params.getBusinessName() || params.getBusinessName().trim().length() == 0) {
            return Result.fail(500,"商家名称不可以为空！");
        } else if (params.getBusinessName().trim().length() > 30) {
            return Result.fail(500,"商家名称长度必须小于30！");
        }


        // 调用业务层（service层）来处理业务逻辑！
        Integer x = businessService.updateBusiness(params);

        return Result.success(params.getBusinessId(),"更新商家成功！");
    }

    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable Integer id) {
        businessService.deleteBusiness(id);
        return Result.success(id,"商家删除成功！");
    }


    @PostMapping("/listBusinessByOrderTypeId")
    public Result listBusinessByOrderTypeId(BusinessReqDto params) {
        System.out.println("-----------business/listBusinessByOrderTypeId...");

        List<Business> businessList = businessService.getBusinessList(params);

        return Result.success(businessList,"根据点餐类别查询商家成功！");
    }






}
